package com.party.admin.web.controller.notify;

import com.google.common.base.Strings;
import com.party.admin.biz.notify.MessageTemplateBizService;
import com.party.admin.web.dto.AjaxResult;
import com.party.admin.web.dto.output.notify.TemplateOutput;
import com.party.common.paging.Page;
import com.party.core.model.activity.Activity;
import com.party.core.model.crowdfund.CrowdfundEvent;
import com.party.core.model.notify.EventChannel;
import com.party.core.model.notify.TargetTemplate;
import com.party.core.model.notify.TargetTemplateType;
import com.party.core.model.notify.TargetTemplateWithQuery;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.crowdfund.ICrowdfundEventService;
import com.party.core.service.crowdfund.ITargetTemplateService;
import com.party.core.service.notify.IEventChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * 消息模板控制器
 * Created by wei.li
 *
 * @date 2017/10/13 0013
 * @time 14:15
 */

@Controller
@RequestMapping(value = "notify/template")
public class MessageTemplateController {

    @Autowired
    private MessageTemplateBizService messageTemplateBizService;

    @Autowired
    private ITargetTemplateService targetTemplateService;

    @Autowired
    private IEventChannelService eventChannelService;

    @Autowired
    private IActivityService activityService;
    @Autowired
    private ICrowdfundEventService crowdfundEventService;

    /**
     * 模板列表
     * @param targetTemplateWithQuery 查询参数
     * @param page 分页参数
     * @return 交互数据
     */
    @RequestMapping(value = "list")
    public ModelAndView list(TargetTemplateWithQuery targetTemplateWithQuery, Page page){
        page.setLimit(10);
        ModelAndView modelAndView = new ModelAndView("/crowdfund/tempaleteList");
        if (null == targetTemplateWithQuery.getSendStatus()) {
            targetTemplateWithQuery.setSendStatus(TargetTemplateWithQuery.SEND_STATUS_NO);
        }
        List<TemplateOutput> list = messageTemplateBizService.list(targetTemplateWithQuery, page);
        Map<Integer, String> types = TargetTemplateType.convertMap(0);
        modelAndView.addObject("list", list);
        modelAndView.addObject("types", types);
        modelAndView.addObject("page", page);
        Activity activity = activityService.get(targetTemplateWithQuery.getTargetId());
        modelAndView.addObject("sendStatus", targetTemplateWithQuery.getSendStatus());
        modelAndView.addObject("targetId", targetTemplateWithQuery.getTargetId());
        modelAndView.addObject("activity", activity);
        return modelAndView;
    }


    /**
     * 活动模板列表
     * @param targetTemplateWithQuery 查询参数
     * @param page 分页参数
     * @return 交互数据
     */
    @RequestMapping(value = "activity/list")
    public ModelAndView activityList(TargetTemplateWithQuery targetTemplateWithQuery, Page page){
        page.setLimit(10);
        ModelAndView modelAndView = new ModelAndView("/activity/tempaleteList");
        if (null == targetTemplateWithQuery.getSendStatus()) {
            targetTemplateWithQuery.setSendStatus(TargetTemplateWithQuery.SEND_STATUS_NO);
        }
        List<TemplateOutput> list = messageTemplateBizService.list(targetTemplateWithQuery, page);
        Map<Integer, String> types = TargetTemplateType.convertMap(1);
        types.remove(TargetTemplateType.ACT_STATUS_ALL.getCode());
        modelAndView.addObject("list", list);
        modelAndView.addObject("types", types);
        modelAndView.addObject("sendStatus", targetTemplateWithQuery.getSendStatus());
        modelAndView.addObject("targetId", targetTemplateWithQuery.getTargetId());
        modelAndView.addObject("page", page);
        return modelAndView;
    }

    /**
     * 模板列表-事项
     * @param targetTemplateWithQuery 查询参数
     * @param page 分页参数
     * @return 交互数据
     */
    @RequestMapping(value = "listForEvent")
    public ModelAndView listForEvent(TargetTemplateWithQuery targetTemplateWithQuery, Page page){
        page.setLimit(10);
        ModelAndView modelAndView = new ModelAndView("/crowdfund/tempaleteListForEvent");
        if (null == targetTemplateWithQuery.getSendStatus()) {
            targetTemplateWithQuery.setSendStatus(TargetTemplateWithQuery.SEND_STATUS_NO);
        }
        List<TemplateOutput> list = messageTemplateBizService.list(targetTemplateWithQuery, page);
        Map<Integer, String> types = TargetTemplateType.convertMap(0);
        modelAndView.addObject("list", list);
        modelAndView.addObject("types", types);
        modelAndView.addObject("page", page);
        CrowdfundEvent crowdfundEvent = crowdfundEventService.get(targetTemplateWithQuery.getTargetId());
        modelAndView.addObject("crowdfundEvent", crowdfundEvent);
        modelAndView.addObject("sendStatus", targetTemplateWithQuery.getSendStatus());
        modelAndView.addObject("targetId", targetTemplateWithQuery.getTargetId());
        return modelAndView;
    }



    @ResponseBody
    @RequestMapping(value = "init")
    public AjaxResult init(){
        List<EventChannel> eventChannelList = eventChannelService.list(new EventChannel());
        for (EventChannel eventChannel : eventChannelList){
            if (!Strings.isNullOrEmpty(eventChannel.getTemplate())){
                String constent = eventChannel.getTemplate().replace("[{partner}]", "").replace("【同行者系统】", "");
                eventChannel.setTemplate(constent);
                eventChannelService.update(eventChannel);
            }
        }

        List<TargetTemplate> targetTemplateList = targetTemplateService.list(new TargetTemplate());
        for (TargetTemplate targetTemplate : targetTemplateList){
            if (!Strings.isNullOrEmpty(targetTemplate.getTemplate())){
                String constant = targetTemplate.getTemplate().replace("[{partner}]", "").replace("【同行者系统】", "");
                targetTemplate.setTemplate(constant);
                targetTemplateService.update(targetTemplate);
            }
        }
        return AjaxResult.success();
    }
}
